<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Callout;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Form;
use Dcat\Admin\Show;
use Dcat\Admin\Widgets\Tab;
use App\Models\Project;
use App\Models\InfoModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;
use App\Models\InformationList;

class InformationModelController extends Controller
{

    public function index(Content $content)
    {
        return $content
            ->header('文件模型列表')
            ->description()
//            ->body(Callout::make('即将在下个版本发布，敬请期待~'))
           // ->body($this->build())
            ->body($this->grid());
    }
    protected function grid($id = 0)
    {
        return new Grid(new InfoModel(), function (Grid $grid) {
            $grid->disableColumnSelector();
            // $grid->content->expand(function (Grid\Displayers\Expand $expand) {
            //     // 设置按钮名称
            //     $expand->button($this->projectname);
            //     return DeviceTable::make();
            // });
            $grid->column('id','ID');
            // $grid->column('projectname','项目名称');
            $grid->column('name','模型名称');
            $grid->column('remark','项目说明')->limit(50);
            $grid->column('created_at','创建时间');
            // $grid->column('user_id','创建人')->display(function($userId) {
            //     return Db::table('admin_users')->where(['id'=>$userId])->value('name');;
            // });
            // 启用边框模式
            $grid->withBorder();
            $grid->fixColumns(2);
            //显示详情按钮
            // $grid->showViewButton();
            $grid->disableViewButton();
            //操作按钮
            $grid->showActions();
            // $grid->disableActions();
            $grid->disableBatchActions();
            //禁用批量操作按钮
            $grid->disableBatchDelete();
            // 禁用创建按钮
            // $grid->disableCreateButton();
            // $url = admin_base_path('Project/create');
            // $grid->tools("<a href='$url' class='btn btn-primary btn-outline'><i class='feather'>新增</a>");
            // 创建按钮
            $grid->showCreateButton();
            // 禁用详情按钮
            // $grid->disableViewButton();
            // 禁用编辑按钮
            // $grid->disableEditButton();
            // 显示编辑按钮
            $grid->showEditButton();
            // 禁用快捷编辑按钮
            $grid->disableQuickEditButton();
            // 显示快捷编辑按钮
            // $grid->showQuickEditButton();
            // 设置弹窗宽高，默认值为 '700px', '670px'
            // $grid->setDialogFormDimensions('50%', '60%');
            // $grid->enableDialogCreate();
            // 禁用删除按钮
            $grid->disableDeleteButton();
            // $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
            //     $create->text('projectname', '项目名称');
            //     $create->text('remark', '项目说明');
            //     $create->select('projectId', '所属公司')->options([
            //         1 => 'foo',
            //         2 => 'bar',
            //     ]);
            //     $create->decimal('lat', '纬度');
            //     $create->decimal('lng', '经度');
            // });
            // 显示删除按钮
            $a = Admin::user()->roles->toArray();
            if($a[0]['id']!=1){ //超级管理员可以删除
                $grid->disableCreateButton();
            }
            $grid->disableEditButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                
                if($id == 1){
                    $edit_url = admin_base_path('RetrofitInfo');
                    $actions->append("<a href='$edit_url'>建表  </a>");
                    $actions->append("<a href='RetrofitInfo/list/1'> 列表</a>");
                }
                if($id == 2){
                    $edit_url = admin_base_path('PowerInstallation');
                    $actions->append("<a href='$edit_url'>建表  </a>");
                    $actions->append("<a href='PowerInstallation/list/2'>  列表</a>");
                }
                if($id == 3){
                    $edit_url = admin_base_path('ProjectDailyReport');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='ProjectDailyReport/list/3'> 列表</a>");
                }
                if($id == 4){
                    $edit_url = admin_base_path('PerformanceTest');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='PerformanceTest/list/4'> 列表</a>");
                }
                if($id == 5){
                    $edit_url = admin_base_path('InstallInspection');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='InstallInspection/list/5'> 列表</a>");
                }
                if($id == 6){
                    $edit_url = admin_base_path('TrainingRecords');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='TrainingRecords/list/6'> 列表</a>");
                }
                if($id == 7){
                    $edit_url = admin_base_path('AfterSalesService');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='AfterSalesService/list/7'> 列表</a>");
                }
                if($id == 8){
                    $edit_url = admin_base_path('EngineeringCommissioning');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='EngineeringCommissioning/list/8'> 列表</a>");
                }
                if($id == 9){
                    $edit_url = admin_base_path('ProjectRegister');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='ProjectRegister/list/9'> 列表</a>");
                }
                if($id == 10){
                    $edit_url = admin_base_path('EquipmentExceptionRecord');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='EquipmentExceptionRecord/list/10'> 列表</a>");
                }
                if($id == 11){
                    $edit_url = admin_base_path('MoveNotes');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='MoveNotes/list/11'> 列表</a>");
                }
                if($id == 12){
                    $edit_url = admin_base_path('ChangeShifts');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='ChangeShifts/list/12'> 列表</a>");
                }
                if($id == 13){
                    $edit_url = admin_base_path('Scheme');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='Scheme/list/13'> 列表</a>");
                }
                if($id == 14){
                    $edit_url = admin_base_path('SpareParts');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='SpareParts/list/14'> 列表</a>");
                }
                if($id == 15){
                    $edit_url = admin_base_path('Technology');
                    $actions->append("<a href='$edit_url'>建表 </a>");
                    $actions->append("<a href='Technology/list/15'> 列表</a>");
                }
                
                // // $actions->append('<a href="/admin/project/ProjectLocation/'.$id.'"><i class="fa fa-location-arrow"></i></a>');
                // $plan = ProjectPlan::where('project_id', $id)->first();
                // $actions->append('<a href="/admin/report/'.$id.'"><i class=" fa fa-sticky-note-o"></i></a>');
                // // print_r($plan->id);die;
                // if(isset($plan)){
                //     $actions->append('<a href="/admin/project/ProjectLocationView/'.$plan->id.'"><i class="fa fa-puzzle-piece"></i></a>');
                //     $actions->append('<a href="/admin/project/ProjectLocation/'.$plan->id.'"><i class="fa fa-magic"></i></a>');
                    
                // }
            });
        });
    }

    public function update($id){
        return $this->form()->update($id);
    }
    public function store(){
        return $this->form()->store();
    }

    public function create(Content $content){
        return $content
            ->title('新增模型')
            ->description('创建')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form());
    }
    public function form(){
        return Form::make(new InfoModel(), function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                // 设置标题
                $form->title('基本设置');
                // 显示底部提交按钮
                $form->showFooter();
                // 设置字段宽度
                $form->width(9, 3);
                // $form->display('user_id', 'ID');
                $form->column(6, function (Form\BlockForm $form) {
                    $form->text('name','模型名称')->required();
                    $form->textarea('remark','说明');
                    // $form->hidden('user_id');
                    // $form->image('planeJPG','平面底图');
                    
                });
            });
        })->saved(function (Form $form) {
            
        });
    }
    public function destroy($projectId){
        return $this->form()->destroy($projectId);
    }
    public function edit($id,Content $content){
        return $content
            ->title('模型编辑')
            ->description('编辑')
            // ->body($this->buildPreviewButton())
            // ->body($this->newline())
            ->body($this->form()->edit($id));
    }

    public function show($id){
        return Show::make($id, new Project(), function (Show $show) {
            // $show->layerId;
            $show->field('name','模型名称');
            $show->field('remark','说明备注');
            $show->created_at;
        });
    }
    public function downloadfile(Request $request,$id){
        $file = InformationList::where('id',$id)->first();
        if($file['type']==11){
            $filestr = public_path("filemodellist/".$file['name'].".xlsx");
            $type = '.xlsx';
        }else{
            $filestr = public_path("filemodellist/".$file['name'].".docx");
            $type = '.docx';
        }
        // $filePath = public_path("filemodellist/".$file['name'].".docx");
        if (!file_exists($filestr)) {
            return response('File not found', 404);
        }
        // return response()->streamDownload(function () {
        //     echo 'success';
        // }, 'laravel-readme.txt');

        // return response()->headers->set('Content-Type', 'application/pdf');
        $res = $request->header();
        
        return response()->download($filestr,$file['name'].$type,$res);
    }
}